首页 > 试题广场 >

逆波兰表达式求值

[编程题]逆波兰表达式求值
  • 热度指数:21923 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个逆波兰表达式,求表达式的值。

数据范围:表达式长度满足 ,表达式中仅包含数字和 + ,- , * , / ,其中数字的大小满足
示例1

输入

["2","1","+","4","*"]

输出

12
示例2

输入

["2","0","+"]

输出

2
class Solution:
    def evalRPN(self , tokens: List[str]) -> int:
        # write code here
        stack = []
        for i in tokens:
            stack.append(i)
            if(stack[-1] in ['+','-','*','/']):
                operator = stack.pop()
                right = stack.pop()
                left = stack.pop()
                stack.append(str(int(eval(left+operator+right))))
        return int(stack[0])

发表于 2022-08-31 07:14:24 回复(0)
C++
class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> stk;
        int tmp1, tmp2;
        for (auto i : tokens)
            if (i[0] < 48 && i.size() == 1)
            {
                tmp1 = stk.top();
                stk.pop();
                tmp2 = stk.top();
                stk.pop();
                if (i[0] == 42)
                    stk.push(tmp2 * tmp1);
                else if (i[0] == 43)
                    stk.push(tmp2 + tmp1);
                else if (i[0] == 45)
                    stk.push(tmp2 - tmp1);
                else
                    stk.push(tmp2 / tmp1);
            }
            else
                stk.push(stoi(i));
            return stk.top();
    }
};
Python
class Solution:
    def evalRPN(self , tokens: List[str]) -> int:
        stk = []
        for i in tokens:
            if ord(i[-1]) < 48:
                tmp = stk.pop()
                stk.append(str(int(eval((stk.pop() + i + tmp)))))
            else:
                stk.append(i)
        return int(stk[-1])


发表于 2022-08-10 21:36:09 回复(0)
class Solution:
    def evalRPN(self , tokens: List[str]) -> int:
        # write code here
        if len(tokens) == 1:
            return int(tokens[0])
        l = []
        x = 0
        y = 0
        for i in tokens:
            if i not in '+-*/':
                l.append(i)
            else:
                x = l.pop()
                y = l.pop()
                # 一定要转为int
                res = int(eval(str(y) + str(i) + str(x)))
                l.append(res)
        return l[0]

发表于 2022-08-08 19:41:02 回复(0)
class Solution:
    def evalRPN(self , tokens: List[str]) -> int:
        # write code here
        if tokens:
            token_list = ["+", "-", "*", "/"]
            stack = []
            for token in tokens:
                if token in token_list:
                    if len(stack) >= 2:
                        first = stack.pop()
                        second = stack.pop()
                        result = eval(str(second) + str(token) + str(first))
                        stack.append(int(result))
                    elif len(stack) == 1:
                        return int(stack[-1])
                else:
                    stack.append(token)
            return int(stack[-1]) if stack else 0
        else:
            return 0
发表于 2022-08-03 14:54:29 回复(0)
class Solution:
    def evalRPN(self , tokens: List[str]) -> int:
        # write code here
        stack = []
        for token in tokens:
            if token == '+':
                stack.append(stack.pop()+stack.pop())
            elif token == '-':
                stack.append(-stack.pop()+stack.pop())
            elif token == '*':
                stack.append(stack.pop()*stack.pop())
            elif token == '/':
                stack.append(int(1/stack.pop()*stack.pop()))
            else:
                stack.append(int(token))
        return stack.pop()

发表于 2022-07-24 23:52:31 回复(0)
num = []
caozuodict = {'+', '-', '*', '/'}
for item in tokens:
    if item in caozuodict:
        a = num.pop()
        b = num.pop()
        if item == '+':
            num.append(a+b)
        elif item == '-':
            num.append(b-a)
        elif item == '*':
            num.append(a*b)
        else:
            num.append(int(b/a))
    else:
        num.append(int(item))
print(num[0])

发表于 2022-07-14 16:07:56 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#

# @param tokens string字符串一维数组 
# @return int整型
#
class Solution:
    def evalRPN(self , tokens: List[str]) -> int:
        # write code here
        def cal(a,b,op):
            if op=="+":
                return a+b
            elif op=="-":
                return a-b
            elif op=='/':
                return int(a/b)
            elif op=="*":
                return a*b
        lst=[]
        for i in tokens:
            if i=="+" or i=="*" or i=="/" or i=="-":
                b=lst.pop()
                a=lst.pop()
                lst.append(cal(a,b,i))
            else:
                lst.append(int(i))
        return int(lst.pop())
            
发表于 2022-04-06 19:18:36 回复(0)
class Solution:
    def evalRPN(self , tokens: List[str]) -> int:
        # write code here
        ls = []
        for i in range(len(tokens)):
            if tokens[i] in ["+","-","*","/"]:
                l1 = ls.pop()
                l2 = ls.pop()
                res = str(int(eval(l2 + tokens[i] + l1)))
                ls.append(res)
            else:
                ls.append(tokens[i])
        return int(ls[-1])

发表于 2022-01-18 22:10:36 回复(1)

问题信息

难度:
9条回答 4841浏览

热门推荐

通过挑战的用户

查看代码